import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUi from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import './assets/icons/iconfont/iconfont.css';
import './mock'
// 引入qs方便后续axios使用post请求数据
import qs from 'qs'
Vue.config.productionTip = false
Vue.use(ElementUi)
Vue.use(axios)

router.beforeEach(async (to,from,next) => {
  const identify = window.sessionStorage.getItem("userInfo")
  let toPath = to.path;
  if (identify === null && toPath !== '/login') {
    // 将用户重定向到登录页面
    next({ path: '/login' });
    console.log(toPath);
  }else{
    next();
  }
})

new Vue({
  router,
  render: h => h(App),
  methods:{
    jsonToObj(data){
      var list = []
      var holdData=JSON.parse(data["data"])
      for(let i=0;i<holdData.length;i++){
        var obj = holdData[i]["fields"]
        obj.pk = holdData[i].pk
        list.push(obj)
      }
      return list
    }
  },
  created(){
    Vue.prototype.$jsonToObj = this.jsonToObj;
    // 安装qs到Vue原型上
    Vue.prototype.$qs = qs;
    Vue.prototype.$axios = axios;
  }
}).$mount('#app')
